<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Member\Controller\InitController;
class FavoriteController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('MemberFavorite');
	}

	public function _empty() {
		$this->index();
	}

	public function index() {
		$type = I('type', 'share');
		$shares = $this->getItemShare(5, 0);
		include template('index');
	}

	/**
	 * 收藏
	 */
	public function add(){
		$id = (int) I('id');
		if($id < 1){$this->error('参数错误');}
		$userid = cookie('_userid');
		$sqlMap = array();
		if(!isset($userid)){$this->error('请先登录',U('Member/Index/login'));}
		$sqlMap['userid'] = $userid;
		//根据id查出该素材的title和url
		$rs = D('Sucai')->where(array('id'=>$id))->field('url,title')->find();
		$sqlMap['url'] = $rs['url'];
		$sqlMap['title'] = $rs['title'];
		$sqlMap['document_id'] = $id;
		$sqlMap['modelid'] = 2;
		$sqlMap['catid'] = 12;
		$sqlMap['dateline'] = NOW_TIME;
		$sqlMap['ip'] = get_client_ip();
		$iddent = 	$sqlMap['iddent'] = $sqlMap['catid'].'-'.$sqlMap['modelid'].'-'.$userid.'-'.$sqlMap['document_id'];
		//查看素材是否被收藏过
		$is_exit = $this->db->where(array('iddent'=>$iddent,'userid'=>$userid))->count();
		if($is_exit > 0){
			$this->error('您已收藏，请勿重复收藏');
		}
		$result = $this->db->add($sqlMap);
		if(!$result){
			$this->error($this->db->getError());
		}
		$this->success('收藏成功');
	}
	
	/*作品收藏*/
	public function collect(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 20;
		$sqlMap = array();
		$sqlMap['userid'] = $this->userid;
		//该用户收藏的素材
		$count = $this->db->where($sqlMap)->count();
		$lists = $this->db->where($sqlMap)->page($pagecurr,$pagesize)->select();
		//根据userid 获取素材的id
		foreach($lists as $key=>$val){
			$lists[$key]['thumb'] = D('Sucai')->getFieldById($val['document_id'],'thumb');
		}
		$pages = page($count,$pagesize);
		include template('history');
	}
	/**
	 * 统计总数
	 * @author xuewl <master@xuewl.com>
	 */
	public function count() {
		$id = (int) I('id');
		$catid = (int) I('catid');
		$modelid = (int) I('modelid');
		if ($id < 1 || $modelid < 1) {
			$result = 0;
		} else {
			$result = $this->db->where(array('modelid' => $modelid, 'catid' => $catid, 'document_id' => $id))->count();
		}
		echo $result;
		exit();
	}

	/**
	 * 获取数据列表
	 * @author xuewl <master@xuewl.com>
	 */
	public function getlist($param) {
		$page = 5;$num = 10;
		if (isset($param['page'])) {
			$page = (int) $param['page'];
			unset($param['page']);
		}
		if (isset($param['num'])) {
			$num = (int) $param['num'];
			unset($param['num']);
		}
		$sqlMap = $param;	
		$lists = $this->db->where($sqlMap)->page($page)->limit($num)->getField('document_id, id', TRUE);
		return $lists;
	}

	/**
	 * 删除收藏数据
	 * @author xuewl <master@xuewl.com>
	 */
	public function del($id = 0) {
		$id = (int) $id;
		$userid = (int) cookie('_userid');
		if ($id < 1) {
			$this->error('参数错误');
		}
		if ($userid < 1) {
			$this->error('请登录后操作');
		}
		$sqlMap = array();
		$sqlMap['id'] = $id;
		$sqlMap['userid'] = $userid;
		$this->db->where($sqlMap)->delete();
		$this->success('收藏删除成功');
	}
}